using UnityEngine;
using UnityEngine.UI;

/****************************************************
// 功能：小地图显示Item
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/4/10 17:48:35
*****************************************************/

public class ItemMapIcon : BaseWindow
{ 
    public float scaler;
    public RectTransform rectTrans;
    public Image imgIcon;
    public Image imgFrame;
   
    
    public MainLogicUnit unit;
    public Vector3 refPos;
    
    public void InitItem(MainLogicUnit unit, Vector3 refPos) {
        this.unit = unit;
        this.refPos = refPos;

        rectTrans.localEulerAngles = new Vector3(0, 0, -45);
        switch(unit.unitType) {
            case UnitTypeEnum.Hero:
                SetSprite(imgIcon, $"Images/player/MiniMap/{unit.unitData.unitConfig.resName}_mapIcon");
                SetSprite(imgFrame, unit.IsTeam(TeamEnum.Blue) ? "Images/player/MiniMap/blueHeroMapFrame" : "Images/player/MiniMap/redHeroMapFrame");
                imgFrame.SetNativeSize();
                break;
            case UnitTypeEnum.Soldier:
                SetSprite(imgIcon, unit.IsTeam(TeamEnum.Blue) ? "Images/player/MiniMap/blueSoldier_mapIcon" : "Images/player/MiniMap/redSoldier_mapIcon");
                imgIcon.SetNativeSize();
                break;
            case UnitTypeEnum.Tower:
                if(unit.IsTeam(TeamEnum.Blue)) {
                    switch(unit.unitData.unitConfig.unitId) {
                        case 1001:
                            SetSprite(imgIcon, "Images/player/MiniMap/blueTower");
                            break;
                        case 1002:
                            SetSprite(imgIcon, "Images/player/MiniMap/blueCrystal");
                            break;
                    }
                }
                else {
                    switch(unit.unitData.unitConfig.unitId) {
                        case 2001:
                            SetSprite(imgIcon, "Images/player/MiniMap/redTower");
                            break;
                        case 2002:
                            SetSprite(imgIcon, "Images/player/MiniMap/redCrystal");
                            break;
                    }
                }
                imgIcon.SetNativeSize();
                break;
            default:
                break;
        }
    }
    
    private void Update() {
        var offset = unit.LogicPos.ConvertViewVector3() - refPos;
        rectTrans.localPosition = new Vector3(offset.x, offset.z, 0) * scaler;
    }

    public void UnInitItem() {
        unit = null;
        refPos = Vector3.zero;
    }
}
